# -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-TODAY OpenERP S.A. <http://www.openerp.com>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from openerp.addons.dm_base.utils import field_boolean_name_cn as boolean_name,field_selection_name as selection_name
from openerp.addons.dm_rubylong import get_rubylong_fields_xml,get_rublong_data_url
from openerp.osv import fields, osv
from datetime import datetime
from openerp.tools import DEFAULT_SERVER_DATE_FORMAT, DEFAULT_SERVER_DATETIME_FORMAT

class sale_order_oem(osv.osv):
    
    _inherit="sale.order"
    
    def _get_rubylong_xml(self, cr, uid, ids, field_names, args, context=None):
        if isinstance(ids, (int, long)):
            ids = [ids]
        res = {}
        for order_id in ids:
            res[order_id] = ''
        data_xml = ''
        for order in self.browse(cr, uid, ids, context=context):
            #当前对象的xml
            data_xml = ''
            #header data
            order_fields = [
                        ('company_id.logo','company_logo'),
                        ('company_id.name','company_id_name'),
                        #代工单号
                        'name',
                        #客户
                        ('partner_id.code','code'),
                        ('partner_id.name','partner_id'),
                        #客户关联单号
                        'client_order_ref',
                        #客户工单号
                        'client_mrp_ref',
                        #交付日期
                        'minimum_planned_date',
                        
                        ('id','order_id'),
                        ]
            create_date = order.create_date and (fields.datetime.context_timestamp(cr, uid,
                                                        timestamp=datetime.strptime(order.create_date, DEFAULT_SERVER_DATETIME_FORMAT),
                                                        context=context)) or ''
            order_fields.append({'create_date':create_date})
            
            for li in order.order_line:
                order_fields.append({'default_code':li.product_id.default_code})
                order_fields.append({'product_id':li.product_id.name})
                order_fields.append({'product_qty':li.product_uom_qty})
            data_xml += get_rubylong_fields_xml(order, 'header', order_fields)
            
            #detail data
            line_fields = [
                        ('sale_line_id.order_id.id','order_id'),   
                        ]
            
            seq = 0
            for line in order.oem_route_wc_ids:
                seq += 1
                line_fields.append({'oper_id':line.routing_wc_id.name})
                line_fields_new = line_fields + [{'code':seq}]
                data_xml += get_rubylong_fields_xml(line, 'detail', line_fields_new)
                
            #write data to file            
            res[order.id] = get_rublong_data_url(order,data_xml,cr.dbname)

        return res            

    _columns = {
            'rubylong_xml_file_oem':fields.function(_get_rubylong_xml, type='text', string='Rubylong xml'),
            }
    
    
#po_super.STATE_SELECTION = STATE_SELECTION_PO    